{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-24T07:35:55.963374Z",
     "start_time": "2020-02-24T07:35:55.333391Z"
    }
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "import math\n",
    "import time\n",
    "import argparse\n",
    "import random\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torch.nn.parameter import Parameter\n",
    "from torch.nn.functional import gumbel_softmax\n",
    "from utils import gumbel_softmax_3d\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-24T07:35:56.529628Z",
     "start_time": "2020-02-24T07:35:56.523988Z"
    }
   },
   "outputs": [],
   "source": [
    "def load_data(path):\n",
    "    with open(path, 'rb') as f:\n",
    "        data = pickle.load(f)\n",
    "    G = nx.from_dict_of_lists(data)\n",
    "    return G"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-24T07:37:05.436111Z",
     "start_time": "2020-02-24T07:37:05.420034Z"
    }
   },
   "outputs": [],
   "source": [
    "def train(args, G):\n",
    "    bs = args.batch_size\n",
    "    n = G.number_of_nodes()\n",
    "    A = nx.to_numpy_matrix(G)\n",
    "    A = torch.from_numpy(A)\n",
    "    A = A.type(torch.float32)\n",
    "    best_log = []\n",
    "    A = A.cuda()\n",
    "\n",
    "    # set parameters\n",
    "    if torch.cuda.is_available():\n",
    "        A = A.cuda()\n",
    "        x = torch.randn(bs, n, 1, device='cuda')*1e-5\n",
    "        x.requires_grad = True\n",
    "    else:\n",
    "        x = torch.randn(bs, n, 1, requires_grad=True)*1e-5\n",
    "\n",
    "    # set optimizer\n",
    "    optimizer = torch.optim.Adam([x], lr=args.lr)\n",
    "\n",
    "    # training\n",
    "    cost_arr = []\n",
    "    for _ in range(args.iterations):\n",
    "        optimizer.zero_grad()\n",
    "        if torch.cuda.is_available():\n",
    "            probs = torch.empty(bs, n, 2, device='cuda')\n",
    "        else:\n",
    "            probs = torch.empty(bs, n, 2)\n",
    "        p = torch.sigmoid(x)\n",
    "        probs[:, :, 0] = p.squeeze()\n",
    "        probs[:, :, -1] = 1-probs[:, :, 0]\n",
    "        logits = torch.log(probs+1e-10)\n",
    "        s = gumbel_softmax_3d(logits, tau=args.tau, hard=args.hard)[:, :, 0]\n",
    "        s = torch.unsqueeze(s, -1)  # size [bs, n, 1]\n",
    "        cost = torch.sum(s)\n",
    "        constraint = torch.sum((1-torch.transpose(s, 1, 2)) @ A @ (1-s))\n",
    "        loss = cost + args.eta * constraint\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        with torch.no_grad():\n",
    "\n",
    "            constraint = torch.squeeze((1-torch.transpose(s, 1, 2)) @ A @ (1-s))\n",
    "            constraint = constraint.cpu().numpy()\n",
    "            idx = np.argwhere(constraint == 0)  # select constraint=0\n",
    "            if len(idx) != 0:\n",
    "\n",
    "                cost = torch.sum(s, dim=1)[idx.reshape(-1,)]\n",
    "                # from size [bs, 1] select constrain=0\n",
    "                cost_arr.append(torch.min(cost.cpu()))\n",
    "                #print('#',_,':',s_[torch.argmin(cost),:,0],'#',torch.min(cost))\n",
    "                \n",
    "            if _ % 100 == 0:\n",
    "                print(_)\n",
    "                if len(cost_arr) != 0:\n",
    "                    print('# {}, cost: {}'.format(_, ((np.sort(cost_arr))[0:8])))\n",
    "                    #print(x.data[torch.argmin(loss),:,0])\n",
    "                else:\n",
    "                    print('Failed!')\n",
    "                    #print(x.data[torch.argmin(loss),:,0])\n",
    "\n",
    "    return cost_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "start_time": "2020-02-24T07:53:24.316Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda\n",
      "0\n",
      "Failed!\n",
      "100\n",
      "Failed!\n",
      "200\n",
      "Failed!\n",
      "300\n",
      "Failed!\n",
      "400\n",
      "Failed!\n",
      "500\n",
      "Failed!\n",
      "600\n",
      "Failed!\n",
      "700\n",
      "Failed!\n",
      "800\n",
      "Failed!\n",
      "900\n",
      "Failed!\n",
      "1000\n",
      "Failed!\n",
      "1100\n",
      "Failed!\n",
      "1200\n",
      "Failed!\n",
      "1300\n",
      "Failed!\n",
      "1400\n",
      "Failed!\n",
      "1500\n",
      "Failed!\n",
      "1600\n",
      "Failed!\n",
      "1700\n",
      "Failed!\n",
      "1800\n",
      "Failed!\n",
      "1900\n",
      "Failed!\n",
      "2000\n",
      "Failed!\n",
      "2100\n",
      "Failed!\n",
      "2200\n",
      "Failed!\n",
      "2300\n",
      "Failed!\n",
      "2400\n",
      "Failed!\n",
      "2500\n",
      "# 2500, cost: [4823. 4877. 4883. 4890. 4902. 4903.]\n",
      "2600\n",
      "# 2600, cost: [4697. 4730. 4737. 4744. 4750. 4751. 4764. 4767.]\n",
      "2700\n",
      "# 2700, cost: [4609. 4616. 4617. 4620. 4620. 4623. 4628. 4630.]\n",
      "2800\n",
      "# 2800, cost: [4525. 4529. 4531. 4533. 4535. 4537. 4538. 4539.]\n",
      "2900\n",
      "# 2900, cost: [4433. 4436. 4439. 4442. 4445. 4447. 4449. 4449.]\n",
      "3000\n",
      "# 3000, cost: [4377. 4378. 4378. 4380. 4380. 4386. 4386. 4386.]\n",
      "3100\n",
      "# 3100, cost: [4304. 4310. 4310. 4313. 4314. 4314. 4315. 4316.]\n",
      "3200\n",
      "# 3200, cost: [4254. 4255. 4258. 4259. 4259. 4264. 4266. 4266.]\n",
      "3300\n",
      "# 3300, cost: [4205. 4206. 4206. 4207. 4208. 4212. 4212. 4212.]\n",
      "3400\n",
      "# 3400, cost: [4152. 4161. 4163. 4165. 4166. 4166. 4169. 4172.]\n",
      "3500\n",
      "# 3500, cost: [4125. 4126. 4126. 4128. 4130. 4131. 4133. 4134.]\n",
      "3600\n",
      "# 3600, cost: [4091. 4094. 4094. 4097. 4097. 4098. 4099. 4099.]\n",
      "3700\n",
      "# 3700, cost: [4053. 4057. 4058. 4060. 4061. 4062. 4064. 4065.]\n",
      "3800\n",
      "# 3800, cost: [4026. 4032. 4036. 4037. 4037. 4037. 4037. 4039.]\n",
      "3900\n",
      "# 3900, cost: [4010. 4011. 4011. 4011. 4012. 4012. 4014. 4015.]\n",
      "4000\n",
      "# 4000, cost: [3982. 3987. 3988. 3989. 3991. 3991. 3993. 3994.]\n",
      "4100\n",
      "# 4100, cost: [3969. 3970. 3971. 3972. 3972. 3973. 3973. 3973.]\n",
      "4200\n",
      "# 4200, cost: [3952. 3953. 3953. 3953. 3953. 3955. 3955. 3955.]\n",
      "4300\n",
      "# 4300, cost: [3936. 3936. 3938. 3938. 3939. 3939. 3941. 3942.]\n",
      "4400\n",
      "# 4400, cost: [3919. 3924. 3925. 3926. 3927. 3928. 3928. 3929.]\n",
      "4500\n",
      "# 4500, cost: [3911. 3912. 3913. 3914. 3915. 3915. 3916. 3916.]\n",
      "4600\n",
      "# 4600, cost: [3905. 3905. 3905. 3906. 3906. 3906. 3906. 3906.]\n",
      "4700\n",
      "# 4700, cost: [3892. 3896. 3896. 3896. 3897. 3897. 3897. 3898.]\n",
      "4800\n",
      "# 4800, cost: [3887. 3889. 3889. 3889. 3889. 3889. 3889. 3889.]\n",
      "4900\n",
      "# 4900, cost: [3881. 3882. 3882. 3882. 3883. 3884. 3884. 3884.]\n",
      "5000\n",
      "# 5000, cost: [3873. 3874. 3876. 3876. 3877. 3878. 3878. 3878.]\n",
      "5100\n",
      "# 5100, cost: [3870. 3870. 3870. 3871. 3871. 3872. 3872. 3873.]\n",
      "5200\n",
      "# 5200, cost: [3863. 3864. 3864. 3866. 3866. 3867. 3867. 3868.]\n",
      "5300\n",
      "# 5300, cost: [3861. 3862. 3862. 3862. 3863. 3863. 3863. 3864.]\n",
      "5400\n",
      "# 5400, cost: [3858. 3859. 3859. 3859. 3859. 3859. 3859. 3860.]\n",
      "5500\n",
      "# 5500, cost: [3855. 3855. 3856. 3856. 3857. 3857. 3857. 3857.]\n",
      "5600\n",
      "# 5600, cost: [3852. 3852. 3853. 3853. 3853. 3853. 3853. 3853.]\n",
      "5700\n",
      "# 5700, cost: [3850. 3850. 3850. 3850. 3850. 3850. 3851. 3851.]\n",
      "5800\n",
      "# 5800, cost: [3846. 3847. 3847. 3847. 3848. 3848. 3848. 3848.]\n",
      "5900\n",
      "# 5900, cost: [3844. 3845. 3846. 3846. 3846. 3846. 3846. 3847.]\n",
      "6000\n",
      "# 6000, cost: [3844. 3844. 3845. 3845. 3845. 3845. 3845. 3845.]\n",
      "6100\n",
      "# 6100, cost: [3842. 3842. 3843. 3843. 3843. 3844. 3844. 3844.]\n",
      "6200\n",
      "# 6200, cost: [3839. 3841. 3841. 3841. 3841. 3842. 3842. 3842.]\n",
      "6300\n",
      "# 6300, cost: [3839. 3839. 3839. 3840. 3841. 3841. 3841. 3841.]\n",
      "6400\n",
      "# 6400, cost: [3838. 3838. 3838. 3838. 3838. 3839. 3839. 3839.]\n",
      "6500\n",
      "# 6500, cost: [3836. 3837. 3837. 3837. 3838. 3838. 3838. 3838.]\n",
      "6600\n",
      "# 6600, cost: [3836. 3837. 3837. 3837. 3837. 3837. 3837. 3837.]\n",
      "6700\n",
      "# 6700, cost: [3836. 3836. 3836. 3836. 3836. 3837. 3837. 3837.]\n",
      "6800\n",
      "# 6800, cost: [3836. 3836. 3836. 3836. 3836. 3836. 3836. 3836.]\n",
      "6900\n",
      "# 6900, cost: [3833. 3834. 3834. 3834. 3835. 3835. 3836. 3836.]\n",
      "7000\n",
      "# 7000, cost: [3833. 3834. 3834. 3834. 3835. 3835. 3835. 3835.]\n",
      "7100\n",
      "# 7100, cost: [3833. 3833. 3834. 3834. 3834. 3834. 3834. 3834.]\n",
      "7200\n",
      "# 7200, cost: [3833. 3833. 3833. 3834. 3834. 3834. 3834. 3834.]\n",
      "7300\n",
      "# 7300, cost: [3833. 3833. 3833. 3833. 3833. 3834. 3834. 3834.]\n",
      "7400\n",
      "# 7400, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7500\n",
      "# 7500, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7600\n",
      "# 7600, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7700\n",
      "# 7700, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7800\n",
      "# 7800, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7900\n",
      "# 7900, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8000\n",
      "# 8000, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8100\n",
      "# 8100, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8200\n",
      "# 8200, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8300\n",
      "# 8300, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8400\n",
      "# 8400, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8500\n",
      "# 8500, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8600\n",
      "# 8600, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8700\n",
      "# 8700, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8800\n",
      "# 8800, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "8900\n",
      "# 8900, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9000\n",
      "# 9000, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9100\n",
      "# 9100, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9200\n",
      "# 9200, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9300\n",
      "# 9300, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9400\n",
      "# 9400, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9500\n",
      "# 9500, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9600\n",
      "# 9600, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9700\n",
      "# 9700, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9800\n",
      "# 9800, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "9900\n",
      "# 9900, cost: [3833. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "# 0, cost: 3833.0\n",
      "0\n",
      "Failed!\n",
      "100\n",
      "Failed!\n",
      "200\n",
      "Failed!\n",
      "300\n",
      "Failed!\n",
      "400\n",
      "Failed!\n",
      "500\n",
      "Failed!\n",
      "600\n",
      "Failed!\n",
      "700\n",
      "Failed!\n",
      "800\n",
      "Failed!\n",
      "900\n",
      "Failed!\n",
      "1000\n",
      "Failed!\n",
      "1100\n",
      "Failed!\n",
      "1200\n",
      "Failed!\n",
      "1300\n",
      "Failed!\n",
      "1400\n",
      "Failed!\n",
      "1500\n",
      "Failed!\n",
      "1600\n",
      "Failed!\n",
      "1700\n",
      "Failed!\n",
      "1800\n",
      "Failed!\n",
      "1900\n",
      "Failed!\n",
      "2000\n",
      "Failed!\n",
      "2100\n",
      "Failed!\n",
      "2200\n",
      "Failed!\n",
      "2300\n",
      "Failed!\n",
      "2400\n",
      "# 2400, cost: [4989. 5021. 5022. 5027.]\n",
      "2500\n",
      "# 2500, cost: [4824. 4845. 4849. 4868. 4871. 4896. 4911. 4918.]\n",
      "2600\n",
      "# 2600, cost: [4687. 4689. 4738. 4747. 4749. 4756. 4763. 4783.]\n",
      "2700\n",
      "# 2700, cost: [4597. 4602. 4609. 4610. 4620. 4627. 4629. 4632.]\n",
      "2800\n",
      "# 2800, cost: [4509. 4520. 4521. 4523. 4525. 4529. 4540. 4543.]\n",
      "2900\n",
      "# 2900, cost: [4434. 4441. 4448. 4449. 4451. 4452. 4456. 4457.]\n",
      "3000\n",
      "# 3000, cost: [4369. 4372. 4376. 4384. 4384. 4386. 4386. 4389.]\n",
      "3100\n",
      "# 3100, cost: [4309. 4309. 4312. 4314. 4314. 4317. 4319. 4321.]\n",
      "3200\n",
      "# 3200, cost: [4246. 4247. 4254. 4254. 4255. 4256. 4256. 4263.]\n",
      "3300\n",
      "# 3300, cost: [4200. 4205. 4207. 4210. 4211. 4213. 4214. 4216.]\n",
      "3400\n",
      "# 3400, cost: [4155. 4155. 4161. 4168. 4168. 4168. 4169. 4170.]\n",
      "3500\n",
      "# 3500, cost: [4121. 4126. 4128. 4131. 4131. 4132. 4133. 4134.]\n",
      "3600\n",
      "# 3600, cost: [4082. 4090. 4092. 4095. 4095. 4097. 4097. 4097.]\n",
      "3700\n",
      "# 3700, cost: [4062. 4064. 4065. 4065. 4065. 4066. 4066. 4067.]\n",
      "3800\n",
      "# 3800, cost: [4031. 4031. 4032. 4032. 4034. 4034. 4036. 4038.]\n",
      "3900\n",
      "# 3900, cost: [3999. 4002. 4007. 4007. 4008. 4010. 4010. 4011.]\n",
      "4000\n",
      "# 4000, cost: [3980. 3983. 3986. 3987. 3990. 3991. 3991. 3992.]\n",
      "4100\n",
      "# 4100, cost: [3966. 3971. 3972. 3973. 3973. 3973. 3975. 3975.]\n",
      "4200\n",
      "# 4200, cost: [3949. 3954. 3954. 3955. 3956. 3956. 3956. 3957.]\n",
      "4300\n",
      "# 4300, cost: [3936. 3938. 3939. 3941. 3941. 3941. 3942. 3943.]\n",
      "4400\n",
      "# 4400, cost: [3922. 3923. 3924. 3925. 3926. 3926. 3927. 3927.]\n",
      "4500\n",
      "# 4500, cost: [3913. 3914. 3914. 3915. 3915. 3915. 3916. 3916.]\n",
      "4600\n",
      "# 4600, cost: [3896. 3902. 3903. 3905. 3905. 3905. 3906. 3906.]\n",
      "4700\n",
      "# 4700, cost: [3895. 3896. 3896. 3897. 3897. 3898. 3898. 3898.]\n",
      "4800\n",
      "# 4800, cost: [3886. 3888. 3888. 3888. 3889. 3890. 3890. 3890.]\n",
      "4900\n",
      "# 4900, cost: [3879. 3881. 3881. 3881. 3882. 3882. 3883. 3883.]\n",
      "5000\n",
      "# 5000, cost: [3875. 3875. 3876. 3877. 3878. 3878. 3878. 3878.]\n",
      "5100\n",
      "# 5100, cost: [3866. 3867. 3869. 3869. 3869. 3870. 3870. 3870.]\n",
      "5200\n",
      "# 5200, cost: [3865. 3865. 3866. 3866. 3866. 3867. 3867. 3867.]\n",
      "5300\n",
      "# 5300, cost: [3860. 3860. 3861. 3861. 3862. 3862. 3862. 3863.]\n",
      "5400\n",
      "# 5400, cost: [3856. 3857. 3857. 3858. 3858. 3858. 3858. 3859.]\n",
      "5500\n",
      "# 5500, cost: [3854. 3854. 3855. 3855. 3855. 3855. 3856. 3856.]\n",
      "5600\n",
      "# 5600, cost: [3851. 3851. 3851. 3851. 3851. 3852. 3852. 3853.]\n",
      "5700\n",
      "# 5700, cost: [3847. 3848. 3849. 3849. 3850. 3850. 3850. 3850.]\n",
      "5800\n",
      "# 5800, cost: [3844. 3845. 3846. 3847. 3847. 3848. 3848. 3848.]\n",
      "5900\n",
      "# 5900, cost: [3844. 3845. 3845. 3846. 3846. 3846. 3846. 3846.]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6000\n",
      "# 6000, cost: [3843. 3843. 3843. 3843. 3843. 3844. 3844. 3844.]\n",
      "6100\n",
      "# 6100, cost: [3839. 3841. 3842. 3842. 3842. 3842. 3842. 3843.]\n",
      "6200\n",
      "# 6200, cost: [3839. 3839. 3839. 3840. 3840. 3840. 3840. 3840.]\n",
      "6300\n",
      "# 6300, cost: [3838. 3839. 3839. 3839. 3839. 3839. 3839. 3839.]\n",
      "6400\n",
      "# 6400, cost: [3836. 3838. 3838. 3839. 3839. 3839. 3839. 3839.]\n",
      "6500\n",
      "# 6500, cost: [3836. 3837. 3837. 3837. 3838. 3838. 3838. 3838.]\n",
      "6600\n",
      "# 6600, cost: [3836. 3837. 3837. 3837. 3837. 3837. 3837. 3838.]\n",
      "6700\n",
      "# 6700, cost: [3835. 3835. 3836. 3836. 3836. 3836. 3836. 3837.]\n",
      "6800\n",
      "# 6800, cost: [3835. 3835. 3835. 3835. 3835. 3835. 3835. 3835.]\n",
      "6900\n",
      "# 6900, cost: [3833. 3834. 3834. 3835. 3835. 3835. 3835. 3835.]\n",
      "7000\n",
      "# 7000, cost: [3833. 3833. 3834. 3834. 3834. 3834. 3834. 3834.]\n",
      "7100\n",
      "# 7100, cost: [3833. 3833. 3833. 3834. 3834. 3834. 3834. 3834.]\n",
      "7200\n",
      "# 7200, cost: [3832. 3833. 3833. 3833. 3833. 3833. 3834. 3834.]\n",
      "7300\n",
      "# 7300, cost: [3832. 3832. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7400\n",
      "# 7400, cost: [3832. 3832. 3832. 3833. 3833. 3833. 3833. 3833.]\n",
      "7500\n",
      "# 7500, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3833.]\n",
      "7600\n",
      "# 7600, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "7700\n",
      "# 7700, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "7800\n",
      "# 7800, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "7900\n",
      "# 7900, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8000\n",
      "# 8000, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8100\n",
      "# 8100, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8200\n",
      "# 8200, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8300\n",
      "# 8300, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8400\n",
      "# 8400, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8500\n",
      "# 8500, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8600\n",
      "# 8600, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8700\n",
      "# 8700, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8800\n",
      "# 8800, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "8900\n",
      "# 8900, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9000\n",
      "# 9000, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9100\n",
      "# 9100, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9200\n",
      "# 9200, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9300\n",
      "# 9300, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9400\n",
      "# 9400, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9500\n",
      "# 9500, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9600\n",
      "# 9600, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9700\n",
      "# 9700, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9800\n",
      "# 9800, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "9900\n",
      "# 9900, cost: [3832. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "# 1, cost: 3832.0\n",
      "0\n",
      "Failed!\n",
      "100\n",
      "Failed!\n",
      "200\n",
      "Failed!\n",
      "300\n",
      "Failed!\n",
      "400\n",
      "Failed!\n",
      "500\n",
      "Failed!\n",
      "600\n",
      "Failed!\n",
      "700\n",
      "Failed!\n",
      "800\n",
      "Failed!\n",
      "900\n",
      "Failed!\n",
      "1000\n",
      "Failed!\n",
      "1100\n",
      "Failed!\n",
      "1200\n",
      "Failed!\n",
      "1300\n",
      "Failed!\n",
      "1400\n",
      "Failed!\n",
      "1500\n",
      "Failed!\n",
      "1600\n",
      "Failed!\n",
      "1700\n",
      "Failed!\n",
      "1800\n",
      "Failed!\n",
      "1900\n",
      "Failed!\n",
      "2000\n",
      "Failed!\n",
      "2100\n",
      "Failed!\n",
      "2200\n",
      "Failed!\n",
      "2300\n",
      "# 2300, cost: [5122.]\n",
      "2400\n",
      "# 2400, cost: [5122.]\n",
      "2500\n",
      "# 2500, cost: [4811. 4841. 4881. 4884. 4885. 4887. 4905. 4955.]\n",
      "2600\n",
      "# 2600, cost: [4698. 4733. 4745. 4749. 4757. 4761. 4777. 4804.]\n",
      "2700\n",
      "# 2700, cost: [4592. 4608. 4619. 4626. 4629. 4633. 4635. 4636.]\n",
      "2800\n",
      "# 2800, cost: [4504. 4517. 4534. 4535. 4537. 4537. 4539. 4540.]\n",
      "2900\n",
      "# 2900, cost: [4430. 4444. 4444. 4444. 4449. 4450. 4452. 4454.]\n",
      "3000\n",
      "# 3000, cost: [4365. 4371. 4376. 4377. 4378. 4383. 4384. 4385.]\n",
      "3100\n",
      "# 3100, cost: [4302. 4304. 4309. 4313. 4314. 4315. 4315. 4317.]\n",
      "3200\n",
      "# 3200, cost: [4242. 4249. 4253. 4255. 4256. 4258. 4261. 4261.]\n",
      "3300\n",
      "# 3300, cost: [4194. 4200. 4204. 4207. 4208. 4210. 4210. 4212.]\n",
      "3400\n",
      "# 3400, cost: [4164. 4164. 4165. 4166. 4167. 4170. 4170. 4172.]\n",
      "3500\n",
      "# 3500, cost: [4125. 4130. 4131. 4131. 4132. 4132. 4133. 4134.]\n",
      "3600\n",
      "# 3600, cost: [4092. 4092. 4093. 4093. 4095. 4095. 4097. 4098.]\n",
      "3700\n",
      "# 3700, cost: [4056. 4060. 4061. 4062. 4065. 4065. 4065. 4065.]\n",
      "3800\n",
      "# 3800, cost: [4022. 4032. 4033. 4035. 4036. 4036. 4036. 4037.]\n",
      "3900\n",
      "# 3900, cost: [3999. 4000. 4006. 4007. 4009. 4011. 4011. 4012.]\n",
      "4000\n",
      "# 4000, cost: [3986. 3989. 3990. 3991. 3993. 3994. 3994. 3994.]\n",
      "4100\n",
      "# 4100, cost: [3958. 3966. 3967. 3968. 3969. 3971. 3972. 3973.]\n",
      "4200\n",
      "# 4200, cost: [3949. 3953. 3953. 3954. 3955. 3955. 3956. 3956.]\n",
      "4300\n",
      "# 4300, cost: [3935. 3937. 3939. 3940. 3940. 3941. 3942. 3942.]\n",
      "4400\n",
      "# 4400, cost: [3923. 3924. 3925. 3926. 3926. 3927. 3927. 3928.]\n",
      "4500\n",
      "# 4500, cost: [3911. 3913. 3914. 3915. 3915. 3916. 3916. 3916.]\n",
      "4600\n",
      "# 4600, cost: [3899. 3902. 3903. 3903. 3904. 3904. 3906. 3906.]\n",
      "4700\n",
      "# 4700, cost: [3892. 3895. 3895. 3897. 3897. 3898. 3898. 3898.]\n",
      "4800\n",
      "# 4800, cost: [3888. 3888. 3889. 3889. 3890. 3890. 3890. 3890.]\n",
      "4900\n",
      "# 4900, cost: [3880. 3880. 3882. 3882. 3883. 3883. 3884. 3884.]\n",
      "5000\n",
      "# 5000, cost: [3875. 3875. 3875. 3875. 3875. 3875. 3875. 3876.]\n",
      "5100\n",
      "# 5100, cost: [3870. 3871. 3872. 3872. 3872. 3872. 3872. 3873.]\n",
      "5200\n",
      "# 5200, cost: [3864. 3865. 3865. 3866. 3866. 3867. 3867. 3867.]\n",
      "5300\n",
      "# 5300, cost: [3860. 3860. 3861. 3862. 3862. 3862. 3862. 3863.]\n",
      "5400\n",
      "# 5400, cost: [3856. 3857. 3857. 3859. 3859. 3859. 3859. 3860.]\n",
      "5500\n",
      "# 5500, cost: [3853. 3853. 3854. 3854. 3855. 3855. 3856. 3856.]\n",
      "5600\n",
      "# 5600, cost: [3848. 3851. 3852. 3852. 3852. 3852. 3852. 3853.]\n",
      "5700\n",
      "# 5700, cost: [3848. 3848. 3848. 3849. 3849. 3849. 3850. 3850.]\n",
      "5800\n",
      "# 5800, cost: [3845. 3846. 3847. 3847. 3847. 3847. 3847. 3847.]\n",
      "5900\n",
      "# 5900, cost: [3844. 3845. 3845. 3845. 3845. 3846. 3846. 3846.]\n",
      "6000\n",
      "# 6000, cost: [3842. 3843. 3844. 3844. 3844. 3844. 3845. 3845.]\n",
      "6100\n",
      "# 6100, cost: [3841. 3841. 3842. 3842. 3842. 3842. 3842. 3842.]\n",
      "6200\n",
      "# 6200, cost: [3837. 3839. 3840. 3840. 3840. 3841. 3841. 3841.]\n",
      "6300\n",
      "# 6300, cost: [3837. 3837. 3838. 3839. 3839. 3839. 3839. 3839.]\n",
      "6400\n",
      "# 6400, cost: [3837. 3837. 3837. 3837. 3837. 3837. 3838. 3838.]\n",
      "6500\n",
      "# 6500, cost: [3837. 3837. 3837. 3837. 3837. 3837. 3837. 3837.]\n",
      "6600\n",
      "# 6600, cost: [3834. 3835. 3835. 3836. 3836. 3836. 3836. 3837.]\n",
      "6700\n",
      "# 6700, cost: [3834. 3834. 3835. 3835. 3835. 3835. 3836. 3836.]\n",
      "6800\n",
      "# 6800, cost: [3832. 3833. 3833. 3834. 3834. 3834. 3835. 3835.]\n",
      "6900\n",
      "# 6900, cost: [3832. 3833. 3833. 3834. 3834. 3834. 3834. 3834.]\n",
      "7000\n",
      "# 7000, cost: [3832. 3833. 3833. 3833. 3833. 3833. 3833. 3834.]\n",
      "7100\n",
      "# 7100, cost: [3832. 3833. 3833. 3833. 3833. 3833. 3833. 3833.]\n",
      "7200\n",
      "# 7200, cost: [3831. 3832. 3832. 3832. 3833. 3833. 3833. 3833.]\n",
      "7300\n",
      "# 7300, cost: [3831. 3832. 3832. 3832. 3832. 3832. 3833. 3833.]\n",
      "7400\n",
      "# 7400, cost: [3831. 3832. 3832. 3832. 3832. 3832. 3832. 3832.]\n",
      "7500\n",
      "# 7500, cost: [3831. 3831. 3831. 3832. 3832. 3832. 3832. 3832.]\n",
      "7600\n",
      "# 7600, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3832. 3832.]\n",
      "7700\n",
      "# 7700, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "7800\n",
      "# 7800, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "7900\n",
      "# 7900, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8000\n",
      "# 8000, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8100\n",
      "# 8100, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8200\n",
      "# 8200, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8300\n",
      "# 8300, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8400\n",
      "# 8400, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8500\n",
      "# 8500, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n",
      "8600\n",
      "# 8600, cost: [3831. 3831. 3831. 3831. 3831. 3831. 3831. 3831.]\n"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    # Training settings\n",
    "    parser = argparse.ArgumentParser(\n",
    "        description='Solving MIS problems (with fixed tau in GS, parallel version)')\n",
    "    parser.add_argument('--batch-size', type=int, default=128,\n",
    "                        help='batch size (default: 128)')\n",
    "    parser.add_argument('--data', type=str, default='pubmed',\n",
    "                        help='data name (default: cora)')\n",
    "    parser.add_argument('--tau', type=float, default=1.,\n",
    "                        help='tau value in Gumbel-softmax (default: 1)')\n",
    "    parser.add_argument('--hard', type=bool, default=True,\n",
    "                        help='hard sampling in Gumbel-softmax (default: True)')\n",
    "    parser.add_argument('--lr', type=float, default=1e-2,\n",
    "                        help='learning rate (default: 1e-2)')\n",
    "    parser.add_argument('--eta', type=float, default=3.,\n",
    "                        help='constraint (default: 5)')\n",
    "    parser.add_argument('--ensemble', type=int, default=10,\n",
    "                        help='# experiments (default: 100)')\n",
    "    parser.add_argument('--iterations', type=int, default=10000,\n",
    "                        help='# iterations in gradient descent (default: 20000)')\n",
    "    parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)')\n",
    "    args = parser.parse_args(args=[])\n",
    "\n",
    "    # torch.manual_seed(args.seed)\n",
    "    use_cuda = torch.cuda.is_available()\n",
    "    device = torch.device(\"cuda\" if use_cuda else \"cpu\")\n",
    "    print(device)\n",
    "\n",
    "    # loading data\n",
    "    G = load_data('./data/ind.' + args.data + '.graph')\n",
    "\n",
    "    for i in range(args.ensemble):\n",
    "        cost = train(args, G)\n",
    "        if len(cost) != 0:\n",
    "            print('# {}, cost: {}'.format(i, min(cost)))\n",
    "        else:\n",
    "            print('Failed!')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
